<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<title>编辑地址</title>
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
		<meta name="description" content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.">
		<link rel="stylesheet" href="lib/weui.min.css">
		<link rel="stylesheet" href="css/jquery-weui.css">
		<link rel="stylesheet" href="css/style.css">
	</head>
	<body ontouchstart>
		<!--编辑地址开始-->
		<header class="wy-header">
			<div class="wy-header-icon-back" onclick="back();"><span></span></div>
			<div class="wy-header-title">编辑地址</div>
		</header>
		<!--编辑地址结束-->
		<!--主体开始-->
		<form action="#" method="get">
		<div class="weui-content">
			<!--收货人信息开始-->
			<div class="weui-cells weui-cells_form wy-address-edit">
				<!--收货人-->
				<div class="weui-cell">
					<div class="weui-cell__hd"><label class="weui-label wy-lab">收货人</label></div>
					<div class="weui-cell__bd"><input class="weui-input" id="consignee" type="text" pattern="[\u4E00-\u9FA5]{2,8}" required="required" placeholder="张三"></div>
				</div>
				<!--手机号-->
				<div class="weui-cell">
					<div class="weui-cell__hd"><label class="weui-label wy-lab">手机号</label></div>
					<div class="weui-cell__bd"><input class="weui-input" id="phone" type="text" required="required" pattern="(1[35678]\d{9})" placeholder="15512341234"></div>
				</div>
				<!--所在地区-->
				<div class="weui-cell"><!--for="name"-->
					<div class="weui-cell__hd"><label class="weui-label wy-lab">所在地区</label></div>
					<div class="weui-cell__bd"><input class="weui-input" id="address" type="text" value="湖北省 武汉市 武昌区" readonly="" data-code="420106" data-codes="420000,420100,420106"></div>
				</div>
				<!--详细地址-->
				<div class="weui-cell">
					<div class="weui-cell__hd"><label class="weui-label wy-lab">详细地址</label></div>
					<div class="weui-cell__bd">
						<textarea class="weui-textarea" id="detailed" placeholder="详细地址"></textarea>
					</div>
				</div>
				<!--设为默认地址-->
				<div class="weui-cell weui-cell_switch">
					<div class="weui-cell__bd">设为默认地址</div>
					<div class="weui-cell__ft"><input class="weui-switch" id="defaultAddress" type="checkbox"></div>
				</div>
			</div>
			<!--收货人信息结束-->
			<!--保存地址，删除地址开始-->
			<div class="weui-btn-area">
				<input type="button" class="weui-btn weui-btn_primary"  id="showTooltips" value="保存此地址"/>
			</div>
			<!--保存地址，删除地址结束-->
		</div>
		</form>
		<!--主体结束-->
	</body>
	<script src="lib/jquery-2.1.4.js"></script>
	<script src="lib/fastclick.js"></script>
	<script type="text/javascript" src="js/jquery.Spinner.js"></script>
	<script type="text/javascript" src="js/layer/layer.js"></script>
	<script src="js/jquery-weui.js"></script>
	<script src="js/city-picker.js"></script>
	<script>
		$(function() {
			FastClick.attach(document.body);
		});

//点击保存地址触发的方法
		$("#showTooltips").click(function(){
		    //获取姓名
			var consignee=$("#consignee").val();
			if(consignee==null||consignee==""){
			    layer.msg("姓名不能为空");
			    return;
			}
			//获取电话
			var phone=$("#phone").val();
            if (phone != "") {
                if (!(/^((1[3|5|7|8][0-9]{9}))$/.test(phone))) {
                    layer.msg("手机号填写的格式不对，请正确填写");
            		 return false;
                 }
			}else{
                layer.msg("手机号不能为空！");
                return;
			}

            //获取所选地区
            var checkaddress=$("#address").val();
            //获取详细地址
            var detailed=$("#detailed").val();
            if(detailed==null||detailed==""){
                layer.msg("详细地址不能为空");
                return;
            }
            //获取是否设为默认地址
			var defaultAddress= "否";
            if($("#defaultAddress").is(":checked")){
                defaultAddress= "是";
			}
			//ajax异步请求，向数据库插入数据
			$.ajax({
				url:"AddressEdit.htm?consignee="+consignee+"&phone="+phone+"&checkaddress="+checkaddress+"&detailed="+detailed+"&defaultAddress="+defaultAddress,
				type:"post",
				success:function (message) {
						if(message==1){
						    layer.msg("添加地址成功！")
							window.location.href="GetAllAddress.htm";
						}
						if(message==2){
                            layer.msg("添加地址失败，请重新添加！")
						}
                },
				error:function () {
                    layer.msg("异步请求失败，请稍后重试！")
                }
			})
		});
	</script>
	<script>
		$("#address").cityPicker({
			title: "选择出发地",
			onChange: function(picker, values, displayValues) {
				console.log(values, displayValues);
			}
		});
        function back() {
            history.back();
        }
	</script>

</html>